package cn.tedu._05mvcboot03.controller;

import cn.tedu._05mvcboot03.common.respose.JsonResult;
import cn.tedu._05mvcboot03.common.respose.StatusCode;
import cn.tedu._05mvcboot03.mapper.MvcArticleMapper;
import cn.tedu._05mvcboot03.pojo.dto.UserArticleDTO;
import cn.tedu._05mvcboot03.pojo.entity.MvcArticle;
import cn.tedu._05mvcboot03.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Api(tags = "02.文章模块")
@RestController
@RequestMapping("/v1/users/")
public class MvcArticleController {
@Autowired
private MvcArticleMapper mvcArticleMapper;
    @ApiOperation("发表文章功能")
    @PostMapping("article")
    public JsonResult insert(@RequestBody @Validated UserArticleDTO userArticleDTO, @ApiIgnore HttpSession session) {
     UserVO userVO = (UserVO) session.getAttribute("user");
//     if (userVO==null){
//         return new JsonResult(StatusCode.NOT_LOGIN);
//     }
        MvcArticle mvcArticle = new MvcArticle();
        BeanUtils.copyProperties(userArticleDTO,mvcArticle);
        mvcArticle.setUserId(userVO.getId());
        mvcArticleMapper.insert(mvcArticle);
        return JsonResult.ok();
    }
}


